<template>
  <el-dialog
    :title="id ? '编辑项目' : '新增项目'"
    :visible.sync="addLineGroupShow"
    :destroy-on-close="true"
    :append-to-body="true"
    width="45%"
    @close="handleClose"
    class="edit-main"
  >
    <el-form
      ref="formData"
      :model="formData"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item label="线路组" prop="lineGroupName">
        <el-input
          type="input"
          style="width: 80%"
          placeholder="请输入内容"
          v-model="formData.lineGroupName"
          maxlength="200"
          show-word-limit
        >
        </el-input>
      </el-form-item>
      <el-form-item label="线路号码" prop="callerIdNumber">
        <el-input
          type="input"
          style="width: 80%"
          placeholder="请输入内容"
          v-model="formData.callerIdNumber"
          maxlength="200"
          show-word-limit
        >
        </el-input>
      </el-form-item>
      <el-form-item label="最大并发量" prop="param">
        <el-input
          type="input"
          style="width: 80%"
          placeholder="请输入内容"
          v-model="formData.param"
          maxlength="200"
          show-word-limit
        >
        </el-input>
      </el-form-item>
      <el-form-item label="单价" prop="price">
        <el-input
          type="input"
          style="width: 80%"
          placeholder="请输入内容"
          v-model="formData.price"
          maxlength="200"
          show-word-limit
        >
        </el-input>
      </el-form-item>
      <el-form-item label="备注" prop="content">
        <el-input
          type="input"
          style="width: 80%"
          placeholder="请输入内容"
          v-model="formData.content"
          maxlength="200"
          show-word-limit
        >
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="onSubmit">提 交</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  addLineInfo,
  editLineInfo,
  lineInfoDetail,
} from "@/api/projectManagement/lineGroupsList.js";
export default {
  name: "addTaskDialog",
  props: {
    addLineGroupDialogShow: {
      type: Boolean,
      default: false,
    },
    questionText: {
      type: String,
      default: "",
    },
    id: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      formData: {
        lineGroupName: "",
        callerIdNumber: "",
        param: "",
        price: "",
        content: "",
      },
      rules: {
        // projectId: [
        //   { required: true, message: "内容不能为空", trigger: "change" },
        // ],
        // taskName: [
        //   { required: true, message: "内容不能为空", trigger: "blur" },
        // ],
      },
      addLineGroupShow: false,
      productList: [],
    };
  },

  watch: {
    addLineGroupDialogShow: {
      handler(val) {
        if (val) {
          this.addLineGroupShow = val;
        }
      },
      immediate: true,
    },
    id: {
      handler(val) {
        if (val) {
          this.gerDetail();
        }
      },
      immediate: true,
    },
  },
  methods: {
    async gerDetail() {
      const { code, data } = await lineInfoDetail(this.id);
      if (code === 200) {
        this.formData = {
          lineGroupName: data?.lineGroupName,
          callerIdNumber: data?.callerIdNumber,
          param: data?.param,
          price: data?.price,
          content: data?.content,
        };
      }
    },

    // 保存
    async onSubmit() {
      const params = {
        ...this.formData,
      };
      if (this.id) {
        params.id = Number(this.id);
      }
      const { code, msg } = await (this.id
        ? editLineInfo(params)
        : addLineInfo(params));
      if (code === 200) {
        this.$message.success(msg);
        this.handleClose();
      } else {
        this.$message.error(msg);
      }
    },

    handleClose() {
      this.addLineGroupShow = false;
      this.formData = {
        lineGroupName: "",
        callerIdNumber: "",
        param: "",
        price: "",
        content: "",
      };
      this.$emit("onCloseAddLineGroupDialog");
    },
  },
};
</script>

<style lang="scss" scoped>
.edit-main {
  ::v-deep .el-textarea__inner {
    height: 150px;
  }
}
</style>
